﻿using Dapper;
using System;
using BCEntity.TMS.WorryFreeMoveSuiteConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.TMS.Transport;

namespace BCData.TMS.WorryFreeMoveSuiteConfig
{
	/// <summary>
	/// 无忧搬家套餐配置
	/// </summary>
	public class WorryFreeMoveSuiteConfigData : IWorryFreeMoveSuiteConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM worryfreemovesuiteconfig";
		private const string QUERY = @"SELECT A.WorryFreeMoveSuiteConfigId,A.Enabled,A.ProvinceCode,A.CityCode,A.CountyCode,A.AgentRecordId,A.CreateAdminId,A.CreateAdminName,A.CreateTime,
											A.UpdateTime,A.EditAdminId,A.SuiteName,A.EditAdminName,A.VehicleBusinessMappingId,A.VehicleTypeId,A.SuiteForCrowdTitle,A.SuiteForCrowdRemark,
											A.SuiteServiceTitle,A.CarryServiceAmount,A.SuiteServiceRemark,A.CouponAmount,
											B.TransportBusiness,C.VehicleTypeName,C.VehicleTypeEnum,C.VehiclePicPath,D.ProvinceName,D.CityName,D.CountyName
										FROM worryfreemovesuiteconfig A
										INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
										INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId
										INNER JOIN County D ON A.CountyCode=D.CountyCode";
		private const string QUERY_VIEW = @"SELECT A.WorryFreeMoveSuiteConfigId,A.Enabled,A.ProvinceCode,A.CityCode,A.CountyCode,A.AgentRecordId,A.CreateAdminId,A.CreateAdminName,A.CreateTime,
												A.UpdateTime,A.EditAdminId,A.SuiteName,A.EditAdminName,A.VehicleBusinessMappingId,A.VehicleTypeId,A.SuiteForCrowdTitle,A.SuiteForCrowdRemark,
												A.SuiteServiceTitle,A.CarryServiceAmount,A.SuiteServiceRemark,A.CouponAmount,
												B.TransportBusiness,C.VehicleTypeName,C.VehicleTypeEnum,C.VehiclePicPath,D.ProvinceName,D.CityName,D.CountyName,E.StartPrice,
												C.VehicleLength,C.VehicleWidth,C.VehicleHeight,C.MaxWeight,C.MaxStere,B.MaxWithCarPeople
											FROM worryfreemovesuiteconfig A
											INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
											INNER JOIN VehicleType C ON A.VehicleTypeId=C.VehicleTypeId
											INNER JOIN County D ON A.CountyCode= D.CountyCode
											INNER JOIN FreightConfiguration E ON A.VehicleTypeId= E.VehicleTypeId AND A.CountyCode= E.CountyCode
											INNER JOIN VehicleCategoryReference F ON E.VehicleCategoryReferenceId= F.VehicleCategoryReferenceId AND F.VehicleCategory= 'VehicleCategory_Box'";

		public WorryFreeMoveSuiteConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查无忧搬家套餐配置是否存在
		public async Task<bool> Exists(int worryFreeMoveSuiteConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", worryFreeMoveSuiteConfigId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(long agentRecordId,string provinceCode,string cityCode,string countyCode, int vehicleBusinessMappingId)
		{
			string sql = string.Format(@"{0} WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId AND AgentRecordId=?AgentRecordId AND ProvinceCode=?ProvinceCode AND CityCode=?CityCode AND CountyCode=?CountyCode", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64, size: 20);
			parameters.Add("?ProvinceCode", provinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", cityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", countyCode?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加无忧搬家套餐配置
		public async Task<WorryFreeMoveSuiteConfigEntity> Add(WorryFreeMoveSuiteConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("worryfreemovesuiteconfig", new string[] { "ProvinceCode", "CityCode", "CountyCode", "AgentRecordId", "CreateAdminId",
				"CreateAdminName", "SuiteName", "VehicleBusinessMappingId", "VehicleTypeId", "SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle",
				"CarryServiceAmount", "SuiteServiceRemark" ,"CouponAmount" });
			sql = string.Format("{0} {1} WHERE A.WorryFreeMoveSuiteConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?ProvinceCode", entity.ProvinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", entity.CityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", entity.CountyCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?AgentRecordId", entity.AgentRecordId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal, size: 10);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CouponAmount", entity.CouponAmount, DbType.Decimal);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改无忧搬家套餐配置
		public async Task<WorryFreeMoveSuiteConfigEntity> Update(WorryFreeMoveSuiteConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("worryfreemovesuiteconfig", new string[] { "ProvinceCode", "CityCode", "CountyCode", "AgentRecordId", "SuiteName", "EditAdminId",
							"EditAdminName", "VehicleBusinessMappingId", "VehicleTypeId", "SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle", "CarryServiceAmount",
							"SuiteServiceRemark","CouponAmount"  }, new string[] { "WorryFreeMoveSuiteConfigId" });
			sql = string.Format("{0} {1} WHERE A.WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", entity.WorryFreeMoveSuiteConfigId, DbType.Int32, size: 11);
			parameters.Add("?ProvinceCode", entity.ProvinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", entity.CityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", entity.CountyCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?AgentRecordId", entity.AgentRecordId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal, size: 10);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CouponAmount", entity.CouponAmount, DbType.Decimal);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除无忧搬家套餐配置
		public async Task<bool> Delete(int worryFreeMoveSuiteConfigId)
		{
			string sql = @"DELETE FROM worryfreemovesuiteconfig WHERE WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", worryFreeMoveSuiteConfigId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取无忧搬家套餐配置
		public async Task<WorryFreeMoveSuiteConfigEntity> GetEntity(int worryFreeMoveSuiteConfigId)
		{
			string sql = string.Format(@"{0} WHERE A.WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", worryFreeMoveSuiteConfigId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigEntity>(sql, parameters);
		}
		public async Task<WorryFreeMoveSuiteConfigEntity> GetEntity(long agentRecordId, string provinceCode, string cityCode, string countyCode, int vehicleBusinessMappingId)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleBusinessMappingId=?VehicleBusinessMappingId AND A.AgentRecordId=?AgentRecordId AND A.ProvinceCode=?ProvinceCode AND A.CityCode=?CityCode AND A.CountyCode=?CountyCode LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64, size: 20);
			parameters.Add("?ProvinceCode", provinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", cityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", countyCode?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取无忧搬家套餐配置列表
		public async Task<IEnumerable<WorryFreeMoveSuiteConfigViewEntity>> GetList(long agentRecordId,string countyCode)
		{
			string sql = string.Format(@"{0} WHERE A.AgentRecordId=?AgentRecordId AND A.CountyCode=?CountyCode AND A.Enabled=1", QUERY_VIEW);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64, size: 20);
			parameters.Add("?CountyCode", countyCode?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryAsync<WorryFreeMoveSuiteConfigViewEntity>(sql,parameters);
		}
		public async Task<IEnumerable<WorryFreeMoveSuiteConfigViewEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE A.Enabled=1", QUERY_VIEW);
			return await this.databaseContext.Master.QueryAsync<WorryFreeMoveSuiteConfigViewEntity>(sql);
		}
		#endregion

		#region 获取无忧搬家套餐配置分页
		public async Task<(IEnumerable<WorryFreeMoveSuiteConfigEntity>, int)> Query(long? agentRecordId, string suiteName, string provinceCode, string cityCode, string countyCode, 
			bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			if (agentRecordId.HasValue)
			{
				wheres.Add("A.AgentRecordId=?AgentRecordId");
				parameters.Add("?AgentRecordId", agentRecordId.Value, DbType.Int64);
			}
			if (!string.IsNullOrWhiteSpace(suiteName))
			{
				wheres.Add("A.SuiteName LIKE CONCAT('%',CONCAT(?SuiteName,'%'))");
				parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(provinceCode))
			{
				wheres.Add("A.ProvinceCode=?ProvinceCode");
				parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(cityCode))
			{
				wheres.Add("A.CityCode=?CityCode");
				parameters.Add("?CityCode", cityCode, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(countyCode))
			{
				wheres.Add("A.CountyCode=?CountyCode");
				parameters.Add("?CountyCode", countyCode, DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("A.Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM worryfreemovesuiteconfig A
										INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
										INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "A.CreateTime" : "A."+sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<WorryFreeMoveSuiteConfigEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用无忧搬家套餐配置
		public async Task<bool> Enabled(int worryFreeMoveSuiteConfigId, long adminId, string fullname)
		{
			string sql = @"UPDATE worryfreemovesuiteconfig SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", worryFreeMoveSuiteConfigId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用无忧搬家套餐配置
		public async Task<bool> Disabled(int worryFreeMoveSuiteConfigId, long adminId, string fullname)
		{
			string sql = @"UPDATE worryfreemovesuiteconfig SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE WorryFreeMoveSuiteConfigId=?WorryFreeMoveSuiteConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigId", worryFreeMoveSuiteConfigId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion
	}
}



